package tongcheng.com.tongcheng.base.fuwu;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import tongcheng.com.tongcheng.ConStans;
import tongcheng.com.tongcheng.R;
import tongcheng.com.tongcheng.adapter.FuwuLeixingAdapter;
import tongcheng.com.tongcheng.base.BaseActivity;
import tongcheng.com.tongcheng.bean.FuwuLeixingBean;
import tongcheng.com.tongcheng.network.CallServer;
import tongcheng.com.tongcheng.network.MyRequest;
import tongcheng.com.tongcheng.network.OnCallBack;
import tongcheng.com.tongcheng.utils.GsonUtil;
import tongcheng.com.tongcheng.utils.Setting;

public class FuWuLeiXingActivity extends BaseActivity {

    @Bind(R.id.back)
    ImageView mBack;
    @Bind(R.id.back_text)
    TextView mBackText;
    @Bind(R.id.title)
    TextView mTitle;
    @Bind(R.id.title_bar)
    RelativeLayout mBg;
    @Bind(R.id.fuwuleixing_btn)
    Button mFuwuleixingBtn;
    public List<FuwuLeixingBean.DataBean> datas = new ArrayList<>();
    @Bind(R.id.fuwuleixing_rc)
    RecyclerView mFuwuleixingRc;
    private FuwuLeixingAdapter mFuwuLeixingAdapter;


    @Override
    public Integer initResid() {
        return R.layout.activity_fu_wu_lei_xing;
    }

    @Override
    public void initCreate(Bundle savedInstanceState) {
        settitlebar(mBg);
        mTitle.setText("服务类型");
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mFuwuleixingRc.setLayoutManager(linearLayoutManager);
        mFuwuLeixingAdapter = new FuwuLeixingAdapter(this, datas);
        mFuwuleixingRc.setAdapter(mFuwuLeixingAdapter);

        final StringBuffer sb = new StringBuffer();
        mFuwuleixingBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (int i = 0; i < datas.size(); i++) {
                    List<FuwuLeixingBean.DataBean.SonListBean> sonList = datas.get(i).sonList;
                    for (int j = 0; j < sonList.size(); j++) {
                        if (sonList.get(j).isChoose) {
                            sb.append(sonList.get(j).name+" ");
                        }
                    }
                }
                Setting.setTemp(1);
                Setting.setFuwuLeixing(sb.toString());
                finish();
            }
        });

        initData();
    }

    private void initData() {
        MyRequest request = new MyRequest(ConStans.FuWuLeixing_Url);
        CallServer.getInstance().addPost(this,request, 1, new OnCallBack() {
            @Override
            public void OnSuccess(int what, String json, Object ject) {
                FuwuLeixingBean fuwuLeixingBean = GsonUtil.GsonToBean(json, FuwuLeixingBean.class);
                List<FuwuLeixingBean.DataBean> data = fuwuLeixingBean.data;
                if (data != null) {
                    datas.addAll(data);
                    if (mFuwuLeixingAdapter!=null) {
                        mFuwuLeixingAdapter.notifyDataSetChanged();
                    }
                }
            }
        });
    }


}
